package com.googlecode.bip.xml.export;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.naming.NamingException;

/**
 * Handle XML export from MySQL database
 */
// not public
class XMLTopic
        implements XMLAppender
{
    private static final long serialVersionUID = 1L;
    private List<XMLTopicLocal> topiclocalList;
    private int idtopic;
    private int order;
    private int isactive;
    private List<XMLTopic> topicList;
    private List<XMLQuestion> questionList;

    public XMLTopic(
            XMLSurveyConnection connection,
            int                 idtopic,
            int                 order,
            int                 isactive
            )
        throws SQLException, NamingException
    {
        this.topiclocalList = XMLTopicLocalFactory.createList( connection, idtopic );
        this.idtopic        = idtopic;
        this.order          = order;
        this.isactive       = isactive;
        this.topicList      = XMLTopicFactory.createList( connection, idtopic );
        this.questionList   = XMLQuestionFactory.createList( connection, idtopic );
    }

    /**
     * <XMP>
     * <!ELEMENT topic         (topiclocal+,idtopic,order,isactive,topic*,question*)>
     * <!ELEMENT idtopic       (#PCDATA)>
     * <!ELEMENT order         (#PCDATA)>
     * <!ELEMENT isactive      (#PCDATA)>
     * </XMP>
     */
    @Override
    public void appendXML( Appendable a ) throws IOException
    {
        a.append( "<topic><!-- (idtopic=" )
         .append( Integer.toString( idtopic ) )
         .append( ") -->\n" );

        XMLAppenderHelper.appendXML( a, topiclocalList );

        XMLAppenderHelper.appendXMLValue( a, "idtopic", idtopic );
        XMLAppenderHelper.appendXMLValue( a, "order", order );
        XMLAppenderHelper.appendXMLValue( a, "isactive", isactive );

        XMLAppenderHelper.appendXML( a, topicList );
        XMLAppenderHelper.appendXML( a, questionList );

        a.append( "</topic><!-- (idtopic=" )
         .append( Integer.toString( idtopic ) )
         .append( ") -->\n" );
    }

}
